/**
 * 职务管理初始化
 */
var Position = {
    id: "PositionTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Position.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '编码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '描述', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '职别', field: 'levelName', align: 'center', valign: 'middle', sortable: true},
        {title: '排序', field: 'weight', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
        	if(row.code){
        		var _html = '<a href="javascript:Position.edit(\''+row.id+'\')" title="修改"><i class="fa fa-pencil"></i></a>';
        		_html += '<a href="javascript:Position.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="fa fa-trash-o"></i></a>';
        		return _html;
        	}else{
        		return "";
        	}
        }}
    ];
};

/**
 * 检查是否选中
 */
Position.check = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Position.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加职务
 */
Position.add = function () {
	this.layerIndex = Sys.open('新增职务',Sys.backendPath + '/position/to_add',['800px', '430px']);
};

/**
 * 打开查看职务详情
 */
Position.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
		if(!this.seItem.code){
			Sys.info("职务分类不可编辑！");
			return;
		}
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
		this.layerIndex = Sys.open('修改职务',Sys.backendPath + '/position/to_update/' + id,['800px', '430px']);
    }
};

/**
 * 删除职务
 */
Position.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
		if(!this.seItem.code){
			Sys.info("职务分类不可编辑！");
			return;
		}
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/position/delete", function (data) {
	            Sys.success("删除成功!");
	            Position.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("positionId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除职务【" + name + "】?",operation);
    }
};

/**
 * 查询职务列表
 */
Position.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['code'] = $("#code").val();
    queryData['levels'] = $("#levels").combobox("getValue");
//    queryData['weight'] = $("#weight").val();
    Position.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Position.initColumn();
    var table = new BSTreeTable(Position.id, Sys.backendPath + "/position/list", defaultColunms);
    table.setExpandColumn(1);
    table.setIdField("id");
    table.setParentIdField("parentId");
    table.setExpandAll(true);
    Position.table = table.init();
});
